package com.ryskoo.basicdata.organization.repository;

import com.ryskoo.basicdata.organization.domain.OrganizationPO;
import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 组织机构缓存类
 * @author thq
 */
@Primary
@Repository
public class OrganizationRedisImpl extends BasicRedisImpl<OrganizationPO> implements IOrganizationRepository {
    @Autowired
    private RedisUtil redisClient;

    @Autowired
    private IOrganizationRepository organizationRepository;
    
    /**
     * 组织机构列表缓存KEY
     */
    private static final String listKey = CacheConstant.BDS_ORGANIZATION_LIST_KEY;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(organizationRepository);
    }

    public OrganizationRedisImpl() {
        super(CacheConstant.BDS_ORGANIZATION_KEY);
    }

    
    /**
     * 重写新增组织机构方法
     * 更新组织机构列表缓存
     */
    @Override
    public void create(OrganizationPO organizationPO) {
        redisClient.remove(listKey);
        super.create(organizationPO);
    }

    
    /**
     * 重写编辑组织机构方法
     * 更新组织机构列表缓存
     */
    @Override
    public void update(OrganizationPO organizationPO) {
        redisClient.remove(listKey);
        super.update(organizationPO);
    }

    
    /**
     * 重写删除组织机构方法
     * 更新组织机构列表缓存
     */
    @Override
    public void delete(OrganizationPO organizationPO) {
        redisClient.remove(listKey);
        super.delete(organizationPO);
    }

    
    /**
     * 重写删除组织机构方法
     * 更新组织机构列表缓存
     */
    @Override
    public void deletes(List<String> needDeleteUuids) {
        redisClient.remove(listKey);
        super.deletes(needDeleteUuids);
    }

	/**
	 * 重写getall方法
	 * 从缓存获取组织机构数据
	 * 其他服务需要使用基础数据时，通过这个方法获取基础数据
	 * @return
	 */
    @SuppressWarnings("unchecked")
	@Override
    public List<OrganizationPO> getAll() {
    	
        //从缓存中获取组织机构列表
        Object object = redisClient.getSession(listKey);

        if (object != null) {
            return (List<OrganizationPO>)object;
        }

        //获取组织机构列表，并放入缓存
  		List<OrganizationPO> list = organizationRepository.getByParentId("");
  		if (list != null && list.size() >0) {
  			for (OrganizationPO organizationPO : list) {
  				List<OrganizationPO> childrenList = organizationRepository.getByParentId(organizationPO.getUuid());
  				organizationPO.setChildrens(childrenList);
  			}
  		}
  		
        //将结果放入缓存
        redisClient.setExpireSession(listKey, list, null);

        return list;
    }
    
	/**
	 * 获取组织机构列表
	 * 树状结构的列表，子组织也会获取
	 * @param parentId
	 *  @return 
	 */
	@Override
	public List<OrganizationPO> getByParentId(String parentId) {
		return organizationRepository.getByParentId(parentId);
	}
}
